﻿/*-----------------------------------------------------------------------------------------------
// Copyright (C) yangrongfeng 
//
// 文件名：        SysUserController.cs
// 文件功能描述：   范例-用户信息管理控制器
//
// 
// 创建标识：       杨荣峰  2016-04-21
//
// 修改标识：
// 修改描述：
//
// 备注：	
// 				
//-----------------------------------------------------------------------------------------------*/

using HYBY.Task.Service.IService;
using HYBY.Task.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Models;
using HYBY.WebFrameWork.Controllers;
using HYBY.WebFrameWork.Mvc;
using HYBY.Task.DataEntity;

namespace Web.Controllers {

    /// <summary>
    /// 系统用户控制器，所有的控制器必须都继承自BaseController
    /// </summary>
    public class SysUserController : BaseController {

        private readonly ISysUserService _service;


        public SysUserController(ISysUserService service) {
            _service = service;
        }

        public ActionResult Index() {
            return View();
        }

        /// <summary>
        /// 根据查询条件按分页的方式返回系统用户数据
        /// </summary>
        /// <param name="search">查询条件及分页信息</param>
        /// <returns></returns>
        public ActionResult GetSysUserList(PageAndSearchInfo search = null) {
            var model = new SysUserListModel();
            model.PageTitle = "首页";
            model.PageSubTitle = "用户管理";
            int RecordCount = 0;
            search.PageSize = 10;
            if (search != null) model.ViewList = _service.GetSysUsers(search.SearchKey,search.PageIndex, 12, out RecordCount);
            ViewBag.RecordCount = RecordCount;
            return PartialView("_SysUserList", model);
        }

        /// <summary>
        /// 新增系统用户信息，首次打开用户界面时显示
        /// </summary>
        /// <returns></returns>
        public ActionResult Create() {
            var model = new SysUserModel();
            return View(model);
        }

        /// <summary>
        /// 新增系统用户信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create(SysUserModel model) {
            if (ModelState.IsValid) {
                SysUserEntity User = new SysUserEntity {
                    UserID = new Random().Next(),
                    UserName = model.UserName,
                    UserAccount = model.UserAccount,
                    DeptID = model.DeptID,
                    Duty = model.Duty,
                    Telphone = model.Telphone,
                    Password = model.Password,
                    Mobile = model.Mobile,
                    Address = model.Address
                };
                try {
                    _service.Save(User);
                    var messages = "创建" + model.UserName + "信息成功.";
                    SuccessNotification(messages);
                }
                catch (Exception ex) {
                    ModelState.AddModelError("", ex.ToString());
                    ErrorNotification(ex.ToString());
                }
            }
            return View(model);
        }

        /// <summary>
        /// 处理AJAX方式提交的用户信息新增操作
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult CreateAsynchronous(SysUserModel model) {
            SysUserEntity User = new SysUserEntity {
                UserID = new Random().Next(),
                UserName = model.UserName,
                UserAccount = model.UserAccount,
                DeptID = model.DeptID,
                Duty = model.Duty,
                Telphone = model.Telphone,
                Password = model.Password,
                Mobile = model.Mobile,
                Address = model.Address
            };
            bool Succeed = _service.Save(User);
            if (Succeed) return Json(new { Succeed = true, Message = "数据保存成功！" });
            return Json(new { Succeed = true, Message = "数据保存失败！" });
        }

        /// <summary>
        /// 处理通过AJAX方式提交的批量数据，需要对前端JSON数组进行包装否则Controller无法接收数据
        /// </summary>
        /// <param name="modelList"></param>
        /// <returns></returns>
        public ActionResult BatchCreate(BatchSysUser modelBatch) {
            SysUserEntity User;
            try {
                foreach (var model in modelBatch.data) {
                    User = new SysUserEntity {
                        UserID = new Random().Next(),
                        UserName = model.UserName,
                        UserAccount = model.UserAccount,
                        DeptID = model.DeptID,
                        Duty = model.Duty,
                        Telphone = model.Telphone,
                        Password = model.Password,
                        Mobile = model.Mobile,
                        Address = model.Address
                    };
                    bool Succeed = _service.Save(User);                    
                }
                return Json(new { Succeed = true, Message = "数据保存成功！" });
            }
            catch (System.Exception) {
                return Json(new { Succeed = true, Message = "数据保存失败！" });
            }            
        }

        public ActionResult Edit(Int32 id) {
            var User = _service.Get(id);
            var model = new SysUserModel {
                UserID = id,
                UserName = User.UserName,
                UserAccount = User.UserAccount,
                DeptID = User.DeptID,
                Duty = User.Duty,
                Telphone = User.Telphone,
                Mobile = User.Mobile,
                Password = User.Password,
                Address = User.Address
            };
            model.PageTitle = "编辑用户信息";
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(SysUserModel model) {
            model.PageTitle = "编辑用户信息";
            if (ModelState.IsValid) {
                SysUserEntity User = new SysUserEntity {
                    UserID = model.UserID,
                    UserName = model.UserName,
                    UserAccount = model.UserAccount,
                    DeptID = model.DeptID,
                    Duty = model.Duty,
                    Telphone = model.Telphone,
                    Password = model.Password,
                    Mobile = model.Mobile,
                    Address = model.Address
                };
                try {
                    _service.Save(User);
                    var messages = "保存" + model.UserName + "信息成功.";
                    SuccessNotification(messages);
                }
                catch (Exception ex) {
                    ModelState.AddModelError("", ex.ToString());
                    ErrorNotification(ex.ToString());
                }
            }
            return View(model);
        }
    }

    /// <summary>
    /// 批量提交数据时需要对前端数据进行包装，否则Controller无法接收数据
    /// </summary>
    public class BatchSysUser {
        public List<SysUserModel> data { get; set; }
    }
}